package com.example.smartgreenhouse.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;


@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {

    // 允许使用socketJs方式访问 即可通过http://IP:PORT/gsas/api/v1/environment/subscribe来和服务端websocket连接
    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gsas/api/v1/environment/subscribe")  // WebSocket连接端点
                .setAllowedOriginPatterns("*")
                .withSockJS();
    }

    // 配置信息代理。定义消息代理，设置消息连接请求的各种规范信息。
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
         // 服务端接收地址的前缀，意思就是说客户端给服务端发消息的地址的前缀。
        registry.setApplicationDestinationPrefixes("/app");  // 应用前缀
        // 表示客户端订阅地址的前缀信息，也就是客户端接收服务端消息的地址的前缀信息
        registry.enableSimpleBroker("/topic");  // 启用内存消息代理，前缀为/topic
    }
}
